<?php

namespace app\common\repositories\repair;

use app\common\dao\repair\RepairLogDao;
use app\common\repositories\BaseRepository;
use app\common\repositories\property\users\PropertyUsersLogRepository;

/**
 * Class RepairLogRepository
 *
 * @package app\common\repositories\repair
 * @mixin RepairLogDao
 */
class RepairLogRepository extends BaseRepository
{

    public function getLogType($id = null)
    {
        $data = [
            1 => '创建工单',
            2 => '更新工单',
            3 => '取消工单',
            4 => '确认工单',
            5 => '派发工单',
            9 => '完成工单',
        ];
        if ($id) {
            return $data[$id] ?? '';
        }
        return $data;
    }

    public function __construct(RepairLogDao $dao)
    {
        $this->dao = $dao;
    }

    public function getPropertyList(array $where, $page, $limit, int $propertyId = null)
    {
        $query = $this->dao->search($where, $propertyId);
        $count = $query->count();
        $list = $query->page($page, $limit)
            ->with([
                'userInfo' => function ($query) {
                    $query->field('id,username');
                    $query->bind(['username' => 'username']);
                }
            ])
            ->order('id', 'desc')
            ->select();
        return compact('list', 'count');
    }

    public function addLog(int $repairId, string $remark, int $propertyId = 0, int $userId = 0)
    {
        $data = [
            'property_id' => $propertyId,
            'user_id' => $userId,
            'remark' => $remark,
            'repair_id' => $repairId,
        ];
        return $this->create($data);
    }

    public function editInfo($info, array $data, int $userId = 0)
    {
        return Db::transaction(function () use ($data, $info, $userId) {
            /** @var PropertyUsersLogRepository $repository */
            $repository = app()->make(PropertyUsersLogRepository::class);
            $repository->addInfo($info['property_id'], $userId, 'INSPECTION.LOG.EDIT', $data);
            return $this->dao->update($info['id'], $data);
        });
    }
    public function batchDelete(array $ids)
    {
        $list = $this->dao->selectWhere([
            ['id', 'in', $ids]
        ]);
        if ($list) {
            foreach ($list as $k => $v) {
                $this->dao->delete($v['id']);
            }
            return $list;
        }
        return [];
    }

    /**
     * 报修日志
     * @param int $propertyId
     * @param int $repairId
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getRepairLog(array $where, int $propertyId = null)
    {
        $query = $this->dao->search($where, $propertyId);
        $count = $query->count();
        $list = $query
            ->field('id,repair_id,log_type,add_time,remark')
            ->order('id', 'desc')
            ->select()->each(function ($item) {
                $item['log_type_msg'] = self::LOG_TYPE['REPAIR.'.$item['log_type']] ?? '-';
            });
        return compact('count', 'list');
    }

    public function getDetail(int $id)
    {
        return $this->dao->getSearch(['id' => $id])
            ->find();
    }

}
